package com.ruoyi.project.tool.swagger.WorkerController;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.annotation.SignatureValidation;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysConfig;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.domain.SysWithdrawal;
import com.ruoyi.project.system.domain.SysWorkerMoney;
import com.ruoyi.project.system.service.ISysConfigService;
import com.ruoyi.project.system.service.ISysUserService;
import com.ruoyi.project.system.service.ISysWithdrawalService;
import com.ruoyi.project.system.service.ISysWorkerMoneyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 申请提现Controller
 *
 * @author jjp
 * @date 2020-04-17
 */
@RestController
@Api(description = "工人申请提现", tags = "工人申请提现")
@RequestMapping("/mobileWorker/withdrawal")
public class WorkWithdrawalController extends BaseController {
    @Autowired
    private ISysWithdrawalService WithdrawalService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysWorkerMoneyService sysWorkerMoneyService;

    @Autowired
    private ISysConfigService sysConfigService;


    /**
     * 查询申请提现列表
     */
    @ApiOperation("查询申请提现列表")
    @SignatureValidation
    @GetMapping("/list")
    public TableDataInfo list(SysWithdrawal sysWithdrawal) {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        sysWithdrawal.setUserId(userId);
        startPage();
        List<SysWithdrawal> list = WithdrawalService.selectSysWithdrawalList(sysWithdrawal);
        return getDataTable(list);
    }

    /**
     * 获取申请提现详细信息
     */
    @ApiOperation("获取申请提现详细信息")
    @SignatureValidation
    @GetMapping(value = "/{withdrawalId}")
    public AjaxResult getInfo(@PathVariable("withdrawalId") Long withdrawalId) {
        return AjaxResult.success(WithdrawalService.selectSysWithdrawalById(withdrawalId));
    }

    /**
     * 新增申请提现
     */
    @ApiOperation("新增申请提现")
    @SignatureValidation
    @RepeatSubmit
    @Log(title = "申请提现", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysWithdrawal sysWithdrawal) {
        String value = (String) sysConfigService.selectConfigByKey("CONFIG_WORKER_RATIO");
        String username = SecurityUtils.getUsername();
        SysUser sysUser = userService.selectUserByUserName(username);
        Long userId = sysUser.getUserId();
        sysWithdrawal.setUserId(userId);
        //账户金额money2 申请提现金额money1
        Double money1 = sysWithdrawal.getMoney();
        Double money2 = sysUser.getMoney();
        if (money2 > money1) {
            BigDecimal money = BigDecimal.valueOf(sysWithdrawal.getMoney());
            Double exchange = Double.valueOf(value);
            sysWithdrawal.setFactMoney(money1 - (money.multiply(BigDecimal.valueOf(exchange)).doubleValue()));
            WithdrawalService.insertSysWithdrawal(sysWithdrawal);
            sysUser.setUserName(username);
            sysUser.setMoney(money2 - money1);
            SysWorkerMoney sysWorkerMoney = new SysWorkerMoney();
            sysWorkerMoney.setUserId(userId);
            sysWorkerMoney.setMoneyType("1");
            sysWorkerMoney.setMoney(money1);
            sysWorkerMoney.setWithdrawalId(sysWithdrawal.getWithdrawalId());
            sysWorkerMoneyService.insertSysWorkerMoney(sysWorkerMoney);
            userService.updateUserMoney(sysUser);
            return AjaxResult.success();
        }
        return AjaxResult.error("账户余额不足，不能提现！");
    }


    /**
     * 删除申请提现
     */
    @ApiOperation("删除申请提现")
    @SignatureValidation
    @Log(title = "申请提现", businessType = BusinessType.DELETE)
    @DeleteMapping("/{withdrawalIds}")
    public AjaxResult remove(@PathVariable Long[] withdrawalIds) {
        return toAjax(WithdrawalService.deleteSysWithdrawalByIds(withdrawalIds));
    }


    /**
     * 查询余额明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:money:list')")
    @ApiOperation("查询余额明细列表")
    @GetMapping("/listWorkerMoney")
    public TableDataInfo list(SysWorkerMoney sysWorkerMoney) {
        sysWorkerMoney.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        startPage();
        List<SysWorkerMoney> list = sysWorkerMoneyService.selectSysWorkerMoneyList(sysWorkerMoney);
        return getDataTable(list);
    }

}
